package com.example.dreamlist2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Random;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

//注册界面
//注册要求：用户名 密码
//返回用户ID 和 邀请码
//直接进入主页
//将用户信息和状态存储在StateInfo中
public class RegisterActivity extends AppCompatActivity {

    private Button reg_cancel;
    private Button reg;
    private User new_user = new User();
    StateInfo stateInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        stateInfo = (StateInfo)getApplication();
        reg = findViewById(R.id.register);
        reg_cancel = findViewById(R.id.reg_cancel);

        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();//注册
            }
        });

        reg_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void register(){
        EditText uName = (EditText)findViewById(R.id.uname);
        EditText Password = (EditText)findViewById(R.id.password1);
        EditText Password2 = (EditText)findViewById(R.id.password2);

        String name = uName.getText().toString();
        if(name.length()<1) {
            Toast.makeText(this,"昵称不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        String pwd = Password.getText().toString();
        String pwd2 = Password2.getText().toString();
        if (pwd.length() < 7){
            Toast.makeText(this,"密码至少6位",Toast.LENGTH_SHORT).show();
            return;
        }
        if (!pwd.equals(pwd2)){
            Toast.makeText(this,"两次密码不相同", Toast.LENGTH_SHORT).show();
            return;
        }
        register_new(name,pwd);
    }

    private void register_new(String uname, String password){
        new_user.setUname(uname);
        new_user.setPassword(password);
        String base = "abcdefghijklmnopqrstuvwxyz0123456789#%&";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        final String[] uid = {null};
        for (int i = 0; i < 8; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        new_user.setUinvite(sb.toString());
        new_user.setUfriend(null);
        new_user.setUid("null");

        new_user.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if(e==null){
                    //Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_LONG).show();
                    stateInfo.setLogin(true);//登录状态为成功
                    new_user.setUid(s);
                    stateInfo.setUserin(new_user);
                    show_uid_invited(); //弹窗显示uid和邀请码
                }else{
                    //Toast.makeText(getApplicationContext(),"注册失败" + e.getMessage(), Toast.LENGTH_LONG).show();
                    Toast.makeText(RegisterActivity.this,"注册失败，请重新注册！", Toast.LENGTH_LONG).show();
                    refresh();
                }
            }
        } );

    }

    //用于显示邀请码的弹窗
    private void show_uid_invited(){
        AlertDialog alertDialog = new AlertDialog.Builder(RegisterActivity.this)
                .setTitle("确认信息")
                .setMessage("您的账号是"+new_user.getUid()+",\n您的邀请码是"+new_user.getUinvite()+"。\n")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Bundle bundle = new Bundle();
                        //todo:确定需要的传参
                        //Utils.launch(RegisterActivity.this, BaseActivity.class, bundle, "");
                        Intent intent = new Intent(RegisterActivity.this, BaseActivity.class);
                        startActivity(intent);
                        finish();
                    }
                })
                .create();
        alertDialog.show();
    }

    //用于刷新
    public void refresh(){
        onCreate(null);
    }

}